package com.itcode.controller;

import com.itcode.pojo.Category;
import com.itcode.pojo.PageBean;
import com.itcode.pojo.Result;
import com.itcode.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增文章分类
     *
     * @param category 文章分类
     * @return 无
     */
    @PostMapping
    public Result add(@RequestBody @Validated(Category.Add.class) Category category) {

        categoryService.add(category);
        return Result.success();
    }

    @GetMapping
    public Result<List<Category>> list(){
        List<Category> categorys = categoryService.findAll();
        return Result.success(categorys);
    }

    /**
     * 获取文章分类详情
     *
     * @param id 分类id
     * @return 分类详情
     */
    @GetMapping("/detail")
    public Result<Category> detail(Integer id) {

        Category category = categoryService.findById(id);
        return Result.success(category);
    }

    /**
     * 修改文章分类
     *
     * @param category
     * @return
     */
    @PutMapping
    public Result update(@RequestBody @Validated(Category.Update.class) Category category) {
        categoryService.update(category);
        return Result.success();
    }

    // todo 新增文章分类删除功能，待文章所有接口实现之后

    /**
     * 删除
     * @param id
     * @return
     */
    @DeleteMapping
    public Result remove(Integer id) {
        categoryService.remove(id);
        return Result.success();
    }
}
